package PTA.javabianchengjichu2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class ex02 {
    public static void main(String[] args) {
        Scanner sc=  new Scanner(System.in);
        int n = sc.nextInt();
        String birth[] = new String[n];//存生日
        String people[] = new String[n];//存身份证号
        for(int i=0;i<n;i++){
            people[i]=sc.next();//把身份证号存入数组中
        }
        while(true) {
            String line = sc.next();
            if (line.equals("sort1")) {
                for (int i = 0; i < n; i++) {
                    birth[i] = people[i].substring(6, 10) + "-" + people[i].substring(10, 12) + "-" + people[i].substring(12, 14);
                }
                Arrays.sort(birth);
                for (int i = 0; i < n; i++) {
                    System.out.println(birth[i]);
                }

            } else if (line.equals("sort2")) {
                for (int i = 0; i < n; i++) {
                    birth[i] = people[i].substring(6, 10) + people[i].substring(10, 12) + people[i].substring(12, 14);
                }
                Arrays.sort(birth);//排序
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        if (people[j].contains(birth[i])) {
                            System.out.println(people[j]);
                        }


                    }
                }


            } else if (line.equals("e")) {
                System.out.println("exit");
                System.exit(0);
            }

        }





    }
}
